package com.csoa.community.activity;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import android.os.AsyncTask;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.csoa.community.R;
import com.csoa.community.bean.FuJianModel;
import com.csoa.community.singletons.GlobalVars;
import com.csoa.community.utils.AndroidUtils;
import com.csoa.community.widget.PullToRefreshBase.OnRefreshListener;
import com.csoa.community.widget.PullToRefreshListView;

public class FuJianActivity extends BaseActivity implements OnClickListener {

	PullToRefreshListView mPullList;
	ListView  mList;
	ProgressBar mProgress;

	MAdapter mAdapter;
	public static List<FuJianModel> mData;

	int preCount = 0;
	protected boolean isReload = true;
	
	@Override
	public void initUI() {
		setContentView(R.layout.fujian);

		mPullList = (PullToRefreshListView) this.findViewById(R.id.fj_list);
		mPullList.setMode(PullToRefreshListView.MODE_PULL_DOWN_TO_REFRESH);
		mList = mPullList.getRefreshableView();
		mProgress = (ProgressBar) this.findViewById(R.id.fj_progress);
		super.initUI();
	}

	@Override
	public void initData() {
		
		if (mData == null) {
			mData = new ArrayList<FuJianModel>();
		}
		mData.clear();
		
		mAdapter = new MAdapter();
		mList.setAdapter(mAdapter);
		mPullList.setOnRefreshListener(new OnRefreshListener() {
			@Override
			public void onRefresh() {
				if(mPullList.hasPullFromTop()) {
					isReload = true;
				}else {
					isReload = false;
				}
				buildRequestGetFuJian();
			}
		});
		
		mList.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				String path = mData.get(position).path;
				File fileAttach = new File(path);
				
				if(fileAttach.exists()) {
					AndroidUtils.fileInvoke(FuJianActivity.this, path);
				}else {
					FuJianActivity.this.showToast("该文件已经删除");
				}
				
//				Intent intent = new Intent(FuJianActivity.this, TongZhiXQActivity.class);
//				intent.putExtra(TongZhiXQActivity.extra_tongzhi, mData.get(position));
//				intent.putExtra(TongZhiXQActivity.extra_position, position);
//				startActivity(intent);
			}
		});
		

		buildRequestGetFuJian();

		super.initData();
	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.tz_search_btn:
			break;
		default:
			break;
		}
	}

	protected void buildRequestGetFuJian() {
		initCenterUI(false);
		new AsyncTask<String, String, List<FuJianModel>>() {
			@Override
			protected List<FuJianModel> doInBackground(String... params) {
				List<FuJianModel> data = new ArrayList<FuJianModel>();
				File f = new File(GlobalVars.getInst().getAttachPath());
				File[] fs = f.listFiles();
				if (fs != null) {
					for (int i = 0; i < fs.length; i++) {
						FuJianModel fm = new FuJianModel();
						fm.id = i + "";
						fm.name = fs[i].getName();
						fm.path = fs[i].getAbsolutePath();
						data.add(fm);
					}
				}
				return data;
			}
			protected void onPostExecute(List<FuJianModel> result) {
				mData.clear();
				mData.addAll(result);
				mPullList.onRefreshComplete();
				initCenterUI(true);
				mAdapter.notifyDataSetChanged();
			};
		}.execute("");
	}

	private void initCenterUI(boolean isShowList) {
		if (isShowList) {
			if (mList.getVisibility() != View.VISIBLE) {
				mList.setVisibility(View.VISIBLE);
			}
			if (mProgress.getVisibility() != View.INVISIBLE) {
				mProgress.setVisibility(View.INVISIBLE);
			}
		} else {
			if (mList.getVisibility() != View.INVISIBLE) {
				mList.setVisibility(View.INVISIBLE);
			}
			if (mProgress.getVisibility() != View.VISIBLE) {
				mProgress.setVisibility(View.VISIBLE);
			}
		}
	}

	private class MAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return mData.size();
		}

		@Override
		public Object getItem(int position) {
			return position;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			Holder1 holder;
			FuJianModel model = mData.get(position);
			if (convertView == null
					|| !(convertView.getTag() instanceof Holder1)) {
				convertView = getLayoutInflater().inflate(
						R.layout.fujian_item, null);
				holder = new Holder1();
				holder.name = (TextView) convertView.findViewById(R.id.fji_name);
			} else {
				holder = (Holder1) convertView.getTag();
			}

			String str = model.name;
			if (str != null) {
				str = str.subSequence(0, str.lastIndexOf(".")).toString();
			} else {
				str = "未知附件名称";
			}
			
			holder.name.setText(str);
			
			return convertView;
		}
	}

	private static final class Holder1 {
		TextView name;
	}

}
